<?php

// 限时抢购

namespace app\mobile\controller;

use think\Config;
use think\Request;
use think\Controller;
use app\mobile\model\Limited as LimitedModel;
use app\mobile\model\LimitedType as LimitedTypeModel;

class Limited extends Controller
{

    /**
     * 限时抢购
     * @param Request $request
     * @param LimitedModel $limited
     * @param LimitedTypeModel $limitedType
     * @return mixed
     */
    public function index(Request $request, LimitedModel $limited, LimitedTypeModel $limitedType)
    {

        $page = $request->param('page', null);

        // 分页配置
        $paging = Config::get('limited');

        // 限时抢购商品
        $limited_goods = $limited->with('LimitedType,ShopGoods')->paginate($paging['limited'])->append(['current_time'])->toArray();

        // 限时抢购剩余时间
        $end_time = (new LimitedTypeModel())->order(['end_time' => 'desc'])->value('end_time');
        $start_time = (new LimitedTypeModel())->order(['end_time' => 'desc'])->value('start_time');

        if (!empty($page)) return ['data' => $limited_goods, 'end_time' => $end_time,'start_time'=>$start_time];

        return $this->fetch('', [
            'data'     => $limited_goods,
            'end_time' => $end_time,
            'start_time' => $start_time,
            'page'     => $paging['limited'],
        ]);

    }
}